package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
import ModelLayer.LoanCon;
import ModelLayer.Loan;
import java.util.Date;

public class LoanCtr
{
    private LoanCon loans;
    public LoanCtr(){
        loans = LoanCon.getInstance(); //Get instance of loan container.
    }

    public Loan search(int serialNumber){ //Search by serial number.
        ArrayList<Loan> tempCon = loans.get();
        for(Loan loan : tempCon){
            if (loan.getCopy().getSerialNumber() == serialNumber) return loan;
        }
        return null;
    }

    public ArrayList<Loan> search(String name){ //Seartch by name.
        ArrayList<Loan> list = new ArrayList<>();
        ArrayList<Loan> tempCon = loans.get(); 
        boolean emptyList = true;
        for(Loan loan : tempCon){
            if (loan.getPerson().getName().equals(name)) list.add(loan);
            emptyList = false;
        }
        if(!emptyList){
            return list;
        }
        else{
            return null;
        }
    }

    public ArrayList overdue(){ //Returns list of over due loans.
        ArrayList<Loan> list = new ArrayList<>();
        ArrayList<Loan> tempCon = loans.get(); 
        Date currentDate = new Date();
        boolean emptyList = true;
        for(Loan loan : tempCon){
            if(currentDate.getMonth() >= (1 + loan.getBorrowDate().getMonth())) list.add(loan);
            emptyList = false;
        }
        if(!emptyList){
            return list;
        }
        else{
            return null;
        }
    }

    public ArrayList<Loan> getLoans(){
        return loans.get();
    }

    public void bookLoan(Person person, Copy copy){ //Books a new loan.
        Loan loan = new Loan(person, copy);
        loans.add(loan);
    }

    /*public void editLoan(int serialNumber){ //Edits an existing loan.
    }*/

    public void returnLoan(int serialNumber){ //Deletes an existing loan.
        if(serialNumber > loans.get().size()){
            loans.get().remove(serialNumber);
        }
    }
}
